/**
 * 
 */
$(document).ready(function(){
	//lấy đường dẫn hiện thời
	var url = location.href;
	var params = '';
	if(url.indexOf("?") > -1){
		params = url.substring(url.indexOf("?")+1);
		url = url.substring(0, url.indexOf("?") );
		
	}
	var arrFilterParams = params.split('&');
	var man_id = '';
	var filter = '';
	var psort = '';
	for(var i = 0; i< arrFilterParams.length;i++ ){
		if(arrFilterParams[i].indexOf("man_id") > -1){
			man_id = arrFilterParams[i];
		}else if (arrFilterParams[i].indexOf("filter") > -1){
			filter = arrFilterParams[i];
		}else if(arrFilterParams[i].indexOf("psort") > -1){
			psort = arrFilterParams[i];
		}
	}
	
	var arrFilterAnchors = $(".filter_box a.ofilter");
	for(var i = 0; i< arrFilterAnchors.length; i++){
		var href = '';
		if(arrFilterAnchors.eq(i).hasClass("manfilter")){
			href = "man_id="+arrFilterAnchors.eq(i).attr("href");
			if(filter != ''){
				href+= "&" + filter;
			}
		}else if(arrFilterAnchors.eq(i).attr("class").indexOf("filtervalue") > -1){
			if(filter != ''){
				href = filter + ";"+ arrFilterAnchors.eq(i).attr("href");
			}else{
				href = "filter=" + arrFilterAnchors.eq(i).attr("href");
			}
			if(man_id != ''){
				href = man_id + "&" + href;
			}
		}
		if(psort != '' ){
			if(href != ''){
				href += "&" + psort;
			}else{
				href += psort;
			}
		}
		href = url + "?" + href;
		arrFilterAnchors.eq(i).attr("href", href);
		
	}
	if(psort != ''){
		if(psort.indexOf("asc") > -1){
			$("#select_sort_box").attr("value", "asc");
		}else{
			$("#select_sort_box").attr("value", "desc");
		}
	}
	var arrDropFilters = $(".filter_box a.btnDrop");
	for(var i = 0; i< arrDropFilters.length; i++){
		
		var newParams = params;
		if(arrDropFilters.eq(i).hasClass("manfilter")){
			
			var re = new RegExp('man_id=[0-9]+', "g");
			newParams = newParams.replace(re, "");
			
			if(newParams[0] == '&') newParams = newParams.substring(1);
			if(newParams.length > 0){
				arrDropFilters.eq(i).attr("href", url + "?" + newParams);
			}else{
				arrDropFilters.eq(i).attr("href", url );
			}
		}else if(arrDropFilters.eq(i).hasClass("filtervalue")){
			
			var re = new RegExp("filter=[0-9;]*");
			var f = re.exec(newParams) + ""; 
			f= f.replace("filter=", "");
			
			var fid = f.split(";");
			var newfilter ="";
			for (var j = 0; j< fid.length; j++){
				if(fid[j] != arrDropFilters.eq(i).attr("href")){
						newfilter += fid[j] + ";";
				}
				
			}
			var reg;
			if(newfilter != '') {
				
				newfilter = newfilter.substring(0, newfilter.length -1);
				newfilter = "filter="+newfilter;
				
			}
			reg = new RegExp("filter=[0-9;]*", "g");
			newParams = newParams.replace(reg, newfilter);
			if(newParams[0] == '&') newParams = newParams.substring(1);
			if(newParams[0] == '&') newParams = newParams.substring(1);
			if(newParams !=''){
				arrDropFilters.eq(i).attr("href", url + "?" + newParams);
			}else{
				arrDropFilters.eq(i).attr("href", url);
			}
		}
	}
	$("#select_sort_box").change(function(){
		var re = new RegExp('psort=[a-z]{0,4}', "g");
		if($(this).attr("value")!= ""){
			if(params.indexOf("psort") > -1){
				params = params.replace(re, "psort=" + $(this).attr("value"));
			}else{
				if(params =='') params+="psort="+$(this).attr("value");
				else params+="&psort="+$(this).attr("value");
			}
		}
		location.href = url+ "?" + params;
	});
});
